import app from "../../app";
import { prop_min_type } from "../../shared/interface";
import { player } from "../fight/body/player";
import { logger } from "../server/logger";
import { prop_base, prop_type } from "./prop_base";
/**
 * 背包
 * 分为
 * 装备背包
 * 道具背包
 */
export class bag {
    items: (prop_base | undefined)[] = [];
    constructor() {

    }
    size() {
        return this.items.length;
    }
    reload(data: bag) {
        for (let i = 0; i < data.items.length; i++) {
            const element = data.items[i] as any;
            if (!element) {
                this.items.push(undefined);
                continue;
            }
            let item = app.createItem(element);
            this.items.push(item);
        }
    }
    /**
     * 整理背包
     */
    sort() {
        let newArry: prop_base[] = [];
        for (let i = 0; i < this.items.length; i++) {
            const element = this.items[i];
            if (element) {
                if (element.num <= 0) {
                    continue;
                }
                newArry.push(element)
            }
        }
        newArry = newArry.sort((a,b)=>{
            if(b && a){
                return b.min_type - a.min_type
            }else{
                return 0
            }
        })
        this.items = newArry;
    }
    getTypeItems(t: any) {
        let list: prop_base[] = []
        for (let index = 0; index < this.items.length; index++) {
            const element = this.items[index];
            if (!element) {
                continue;
            }
            if (element.num <= 0) {
                continue;
            }
            if (element instanceof t) {
                list.push(element);
            }
        }
        return list;
    }
    add(new_item: prop_base) {
        // 检测道具是否可以叠加
        // 如果可以叠加  寻找背包是否有相同类型的道具  有则增加数量  没有则push
        // 如果不能叠加  直接push
        let findIdx = -1;
        if (new_item.min_type == prop_min_type.装备) {
            findIdx = -1;
        } else if (!new_item.haveSup) {
            findIdx = -1;
        } else {
            findIdx = this.items.findIndex((item) => {
                return item && item.min_type == new_item.min_type && item.cfg_id == new_item.cfg_id;
            })
        }

        if (findIdx == -1) {
            this.items.push(new_item);
        } else {
            (this.items[findIdx] as prop_base).num += new_item.num;
        }
    }
    getItem_cfgId(id: string) {
        let item = this.items.find((item) => {
            return item && item.cfg_id == id
        })
        return item as prop_base;
    }
    getItem_cfgType(id: string) {
        let item = this.items.find((item) => {
            return item && item.cfg_id.includes(id)
        })
        return item as prop_base;
    }
    get(idx: number, num: number) {
        let getItem = this.items[idx];
        if (!getItem) {
            return undefined;
        }
        if (getItem.num < num) {
            return undefined;
        }
        return getItem;
    }
    test() {
        // 使用
        // intpu idx num player
        let p1 = new player();
        p1.name = '客方';
        p1.uuid = '111'
        let bag = p1.getBag();


        let useCont = 2;
        let useIndex = 0;
        let useItem = bag.get(useIndex, useCont);
        if (!useItem) {
            logger.debug('道具ID不存在或数量不足');
            return;
        }

        let useJude = useItem.use(p1, useCont);
        if (!useJude) {
            return;
        }
        // bag.del(useItem.uuid,useCont);

        bag.sort();
        logger.debug(bag)
    }
}